package top.lc951.myandroid;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * 屏幕旋转
 * <p>
 * 默认生命周期 onCreate  onStart onResume
 * 旋转之后onPause onStop onDestroy
 * 重新创建 onCreate onStart onResume
 * 界面发现旋转变化
 * <p>
 * 1、配置AndroidMainfest android:configChanges="orientation|keyboard"
 * 回调onConfigurationChanged
 *
 */
public class OrientationActivity extends AppCompatActivity {
    public static final String TAG = OrientationActivity.class.getSimpleName();

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, OrientationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate called!");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orientation);
    }

    @Override
    protected void onStart() {
        Log.d(TAG, "onStart called!");

        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "onStop called!");

        super.onStop();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume called!");

        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause called!");

        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy called!");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart called!");

    }

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d(TAG,"ORIENTATION_LANDSCAPE");
            // Nothing need to be done here
        } else {
            Log.d(TAG,"ORIENTATION_PORTRAIT");
            // Nothing need to be done here
        }
    }

    public void changeLandscape(View view){
        //横屏设置
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }

    public void changePortrait(View view){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}